import tkinter as tk  
from tkinter import messagebox  
import pyodbc  
  
# 数据库连接字符串，请根据你的数据库配置进行修改  
conn_str = (  
    r'DRIVER={SQL Server Native Client 11.0};'  
    r'SERVER=PC17;'  
    r'DATABASE=users;'  
    r'Trusted_Connection=Yes;'  
)  
# 连接数据库  
def connect_to_db():  
    try:  
        conn = pyodbc.connect(conn_str)  
        return conn  
    except pyodbc.Error as e:  
        print("连接到SQL Server时出错:", e)  
        return None  
  
# 注册新用户  
def register_user(username, password):  
    conn = connect_to_db()  
    if conn:  
        cursor = conn.cursor()  
        try:  
            # 插入新用户到users表  
            cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))  
            conn.commit()  
            messagebox.showinfo("成功", "用户注册成功！")  
        except pyodbc.Error as e:  
            print("向SQL Server插入数据时出错:", e)  
            messagebox.showerror("错误", "注册用户失败！")  
        finally:  
            cursor.close()  
            conn.close()  
  
# 验证登录  
def login_user(username, password):  
    conn = connect_to_db()  
    if conn:  
        cursor = conn.cursor()  
        try:  
            # 验证用户名和密码  
            cursor.execute("SELECT * FROM users WHERE username = ? AND password = ?", (username, password))  
            row = cursor.fetchone()  
            if row:  
                messagebox.showinfo("成功", "登录成功！")  
            else:  
                messagebox.showerror("错误", "用户名或密码错误！")  
        except pyodbc.Error as e:  
            print("从SQL Server检索数据时出错:", e)  
            messagebox.showerror("错误", "登录失败！")  
        finally:  
            cursor.close()  
            conn.close()  
  
# 创建GUI  
root = tk.Tk()  
root.title("注册与登录")  
  
# 注册部分  
register_frame = tk.LabelFrame(root, text="注册")  
register_frame.pack(pady=20)  
  
tk.Label(register_frame, text="用户名:").grid(row=0, column=0)  
register_username_entry = tk.Entry(register_frame)  
register_username_entry.grid(row=0, column=1)  
  
tk.Label(register_frame, text="密码:").grid(row=1, column=0)  
register_password_entry = tk.Entry(register_frame, show='*')  
register_password_entry.grid(row=1, column=1)  
  
register_button = tk.Button(register_frame, text="注册", command=lambda: register_user(register_username_entry.get(), register_password_entry.get()))  
register_button.grid(row=2, column=1)  
  
# 登录部分  
login_frame = tk.LabelFrame(root, text="登录")  
login_frame.pack(pady=20)  
  
tk.Label(login_frame, text="用户名:").grid(row=0, column=0)  
login_username_entry = tk.Entry(login_frame)  
login_username_entry.grid(row=0, column=1)  
  
tk.Label(login_frame, text="密码:").grid(row=1, column=0)  
login_password_entry = tk.Entry(login_frame, show='*')  
login_password_entry.grid(row=1, column=1)  
  
login_button = tk.Button(login_frame, text="登录", command=lambda: login_user(login_username_entry.get(), login_password_entry.get()))  
login_button.grid(row=2, column=1)  
  
root.mainloop()